#include "cainiaojiaocheng.h"

#include <httpclient.h>

CaiNiaoJiaoCheng::CaiNiaoJiaoCheng()
{
    m_interactive = 0;
    foreach (auto var, allcategories()) {
        m_categories_pages.insert(var, QStringList());
    }
}

/**
 * @brief CaiNiaoJiaoCheng::allcategories
 * @since https://cainiaojiaocheng.com/api.php?action=help&modules=query
 * @since https://cainiaojiaocheng.com/api.php?action=help&modules=query%2Ballcategories
 * @link  https://cainiaojiaocheng.com/api.php?action=query&format=json&prop=info&list=allcategories&aclimit=max
 * @return 分类列表
 */
QStringList CaiNiaoJiaoCheng::allcategories()
{
    QStringList list;
    Request req("https://cainiaojiaocheng.com/api.php");
    req.addQuery("action","query");
    req.addQuery("format","json");
    req.addQuery("prop","info");
    req.addQuery("list","allcategories");
    req.addQuery("aclimit","max");
    auto allcategories = HttpClient::instance().get(req).toMap().value("query").toMap().value("allcategories").toList();
    foreach (auto category, allcategories) {
        list << category.toMap().value("*").toString();
    }
    return list;
}

/**
 * @brief CaiNiaoJiaoCheng::categories_pages
 * @since https://cainiaojiaocheng.com/api.php?action=help&modules=query
 * @since https://cainiaojiaocheng.com/api.php?action=help&modules=query%2Bcategorymembers
 * @link  https://cainiaojiaocheng.com/api.php?action=query&format=json&prop=info&list=categorymembers&cmtitle=分类:VPN&cmlimit=max
 * @return 所属分类所有文章
 */
QStringList CaiNiaoJiaoCheng::categories_pages(const QString &category)
{

    if (m_categories_pages.contains(category)) {
        if (m_categories_pages.value(category).isEmpty() == false) {
            return m_categories_pages.value(category);
        }
    }

    QStringList list;
    Request req("https://cainiaojiaocheng.com/api.php");
    req.addQuery("action","query");
    req.addQuery("format","json");
    req.addQuery("prop","info");
    req.addQuery("list","categorymembers");
    req.addQuery("cmtitle", "分类:"+category);
    req.addQuery("cmlimit","max");
    auto categorymembers = HttpClient::instance().get(req).toMap().value("query").toMap().value("categorymembers").toList();
    foreach (auto categorymember, categorymembers) {
        list << categorymember.toMap().value("title").toString();
    }
    return list;
}

QString CaiNiaoJiaoCheng::name()
{
    return "菜鸟教程 - Wiki";
}

QString CaiNiaoJiaoCheng::prefix()
{
    return "https://cainiaojiaocheng.com";
}

QStringList CaiNiaoJiaoCheng::searching(const QString &text)
{
    QStringList list;

    if (m_interactive == 0) {
        if (text.isEmpty()) {
            return m_categories_pages.keys();
        }

        foreach (auto key, m_categories_pages.keys()) {
            if (key.contains(text, Qt::CaseInsensitive)) {
                list << key;
            }
        }
        return list;
    }

    if (m_interactive == 1) {
        if (m_key == text) {
            return m_categories_pages.value(m_key);
        }

        foreach (auto item, m_categories_pages.value(m_key)) {
            if (item.contains(text, Qt::CaseInsensitive)) {
                list << item;
            }
        }
    }

    return list;
}


QString CaiNiaoJiaoCheng::content(const QString &text)
{
    // auto url = this->prefix() + "/分类:" + m_key;
    // auto items = HttpClient::instance().get(url.replace(" ", "_")).toMap().value("query").toMap().value("categorymembers").toList();

    return HttpClient::instance().get(prefix() + "/" + text).content();;
}

bool CaiNiaoJiaoCheng::interactive(const QString &text)
{
    if (m_interactive == 0) {
        m_key = text;

        if (m_categories_pages.value(m_key).isEmpty()) {
            m_categories_pages.insert(m_key, categories_pages(m_key));
        }

        return m_interactive++ < 1;
    }

    return false;
}

bool CaiNiaoJiaoCheng::interactive_running() {
    return m_interactive != 0;
}

void CaiNiaoJiaoCheng::interactive_restore() {
    m_interactive = 0;
}
